<?php
class ArticleCategoryTable
{
    public static function GetCategories( $language = null )
    {
        $categories = array();

        $sql = "SELECT * FROM `article_category`";
        if ( !empty($language) )
        {
            $sql .= " WHERE language = " . Util::QuoteString($language);
        }
        $data = Core::DBConn()->Query( $sql )->GetAllAssoc();

        foreach ($data as $item)
        {
            $categories[] = self::CreateArticleCategory( $item );
        }
        
        return $categories;
    }

    public static function GetCategoryById( $categoryId )
    {
        $sql = "SELECT * FROM `article_category` WHERE article_category_id = %s LIMIT 1";
        $data = Core::DBConn()->Query( $sql, Util::Quote($categoryId) )->GetAssoc();

        if ( !empty( $data ) )
        {
            return self::CreateArticleCategory( $data );
        }
        return null;
    }

    public static function GetCategoryByName( $name )
    {
        $sql = "SELECT * FROM `article_category` WHERE name = %s LIMIT 1";
        $data = Core::DBConn()->Query( $sql, Util::Quote($name) )->GetAssoc();

        if ( !empty( $data ) )
        {
            return self::CreateArticleCategory( $data );
        }
        return null;
    }

    public static function SaveCategory( ArticleCategory $category )
    {
        if ( $category->GetCategoryId() )
        {
            self::Update( $category );
        }
        else
        {
            self::Insert( $category );
        }

        return $category->GetCategoryId();
    }

    public static function Insert(ArticleCategory $category)
    {
        $sql = 'INSERT INTO article_category
                    (name, language)
                VALUE
                    (@name, @language)';

        $params = array
        (
            '@name'     => $category->GetName(),
            '@language' => $category->GetLanguage()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        $id = Core::DBConn()->GetLastInsertId();

        $category->SetCategoryId($id);

        return $id;
    }

    public static function Update(ArticleCategory $category)
    {
        $sql = 'UPDATE article_category
                SET
                  name = @name,
                  language = @language
                WHERE article_category_id = @category_id';

        $params = array
        (
            '@name'        => $category->GetName(),
            '@language'    => $category->GetLanguage(),
            '@category_id' => $category->GetCategoryId()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        return $category->GetCategoryId();
    }

    public static function Delete( $categoryId )
    {
        Core::DBConn()->Query( 'DELETE FROM article_category WHERE article_category_id = @category_id',
            Util::QuoteArray( array('@category_id' => $categoryId) )
        );
    }

    private static function CreateArticleCategory( $data )
    {
        $category = new ArticleCategory();

        $category->SetCategoryId($data['article_category_id']);
        $category->SetName($data['name']);
        $category->SetLanguage($data['language']);
        
        return $category;
    }
}
?>
